package com.motu.vertx.module.utility.webhandler;


import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import com.motu.vertx.module.utility.webtool.BaseWebHandler;
import com.motu.vertx.module.utility.webtool.SearchCondition;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.client.WebClient;
import io.vertx.sqlclient.Pool;

import java.util.ArrayList;
import java.util.List;

public class FeedbackHandler extends BaseWebHandler{
	public static final String tableName = "feedback";
	private static Pool jdbcClientServer = null;

	public static void init(Pool jdbcServer){
		jdbcClientServer = jdbcServer;
	}
	public static void list(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		getTableList(routingContext, jdbcClientServer,tableName);
	}
	
	public static void update(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		json.put("gmname",adminName);
		json.put("dealtime", TimeUtils.getTimeStrIntValue(json.getString("dealtime")));
		updateToTable(routingContext,jdbcClientServer, tableName,json);
	}

	public static void getone(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		getFromTable(routingContext,jdbcClientServer, tableName);
	}
	public static void search(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		List<SearchCondition> scList = new ArrayList<SearchCondition>();
		scList.add(new SearchCondition("replied", "=", new String[]{"a.replied"}, null));
		scList.add(new SearchCondition("userid", "=", new String[]{"a.userid"}, null));
		scList.add(new SearchCondition("username", "=", new String[]{"a.username"}, null));
		scList.add(new SearchCondition("gmname", "=", new String[]{"a.gmname"}, null));
		scList.add(new SearchCondition("key", "like", new String[]{"a.content","a.response"}, null));
		JsonArray params1 = new JsonArray();
		String str_where = " from feedback a,alluser b where a.userid=b.userid " + getSearchString(scList, json, params1);
		if (json.containsKey("orderby")) {
			str_where += " order by " + json.getString("orderby") + " desc";
		} else {
			str_where += " order by a.id desc";
		}
		String sql1 = "select count(1)" + str_where;
		String sql2 = "select a.*,b.vip" + str_where + " limit ?,?; ";
		JsonArray params2 = params1.copy();
		int pageNo = json.getInteger("pageNo");
		int pageSize = json.getInteger("pageSize");
		int start = pageNo*pageSize;
		params2.add(start);
		params2.add(pageSize);
		searchFromTable(routingContext,jdbcClientServer, tableName, sql1, params1, sql2, params2);
	}
}
